import goodsData from '@/mock/goodsData.JSON'

//从地址栏信息获取query
function queryItems (url) {
  const queryItem = url.split('?')[1]
  if (!queryItem) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(queryItem)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"') +
    '"}'
  )
}

let goodsShow = []
export default{
  getGoods:()=>{
    let {categoryname, keyword, pageNo} = queryItems (window.location.href)
    let start = (pageNo-1)*8
    //categoryname, keyword都有的情况
    if(categoryname && keyword){
      goodsShow = goodsData.filter(item=>item.sort.indexOf(categoryname) != -1).filter(item=>item.sort.indexOf(keyword) != -1)
    }else{
      //categoryname有的情况
      goodsShow =categoryname?goodsData.filter(item=>item.sort.indexOf(categoryname) != -1):goodsData
      //keyword有的情况
      keyword =keyword?goodsData.filter(item=>item.sort.indexOf(keyword) != -1):goodsData

    }
    goodsShow = goodsShow.splice(start,8)
    return {
      code:200,
      msg:'success',
      data:{goodsShow, pageNo, totalPage:Math.floor(goodsShow.length/8)+1 }//pageNo当前页 totalPage 总页数
    }
  }
}